package com.weiyi.servlet03;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * 
 * @description 处理多文件上传
 * @author 惟一
 * @date 2020年6月26日下午5:53:51
 */
@MultipartConfig(location="E://")
@WebServlet("/upload3.do")
public class UploadDemo3 extends HttpServlet{
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		Collection<Part> parts = req.getParts();
		for (Part part : parts) {
			if(part.getName().startsWith("file")) {
				String filename = getFilename(part);
				part.write(filename);
			}		
		}			
	}

	private String getFilename(Part part) {
		String header = part.getHeader("Content-Disposition");
		String filename = header.substring(
				header.indexOf("filename=\"")+10, 
				header.lastIndexOf("\"")
				);	
		return filename;
	}
}
